clear

** Domestic Inventors
import delim using "./input/migration_of_inventors/usborn_city_totpat_fyear_fmonth.csv"

gen 	f_myr = ym(fyear, fmonth)
format 	f_myr %tm
xtset 	gen_id f_myr	
tsfill, full

drop fyear fmonth

replace totpat_usborn = 0 if mi(totpat_usborn)

tempfile domestic
save "`domestic'", replace
clear

** Local Stayers Data 1916-1920
import delim using "./input/migration_of_inventors/stayers_city_totpat_fyear_fmonth.csv"

gen 	f_myr = ym(fyear, fmonth)
format 	f_myr %tm
xtset 	gen_id f_myr	
tsfill, full

drop fyear fmonth

replace totpat_stayers = 0 if mi(totpat_stayers)

tempfile shortinv
save "`shortinv'", replace
clear

** Local Stayers Data 1910-1926
import delim using "./input/migration_of_inventors/stayers_city_totpat_fyear_fmonth_1910_1926.csv"

gen 	f_myr = ym(fyear, fmonth)
format 	f_myr %tm
xtset 	gen_id f_myr	
tsfill, full

drop fyear fmonth

replace totpat_stayers = 0 if mi(totpat_stayers)
rename 	totpat_stayers totpat_stayers_long

merge 1:1 gen_id f_myr using "`shortinv'"
drop _merge

merge 1:1 gen_id f_myr using "`domestic'"
drop _merge

**
compress
save 	"./output/citiesall_patents_migration.dta", replace
clear